#!/usr/bin/python
# encoding=utf-8
'''
pip install PyMySQL

https://pymysql.readthedocs.io/en/latest/
'''
import time
import pymysql

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='abcd4321',
                             db='my-all-in',
                             charset='utf8mb4',
                             autocommit=True)

try:
    with connection.cursor() as cursor:
        while True:
            cursor.execute('''
                select count(1) from t_job_task where type = 'YBC_NEW_REGISTER' and status in ('ING', 'NEW')
            ''')
            qty = cursor.fetchone()[0]
            print('qty: ' + str(qty))
            if qty <= 3:
                i_rows = cursor.execute(
                    '''
                        insert into t_job_task(type, biz, status, done)
                        values
                        ('YBC_NEW_REGISTER', '{}', 'NEW', 0),
                        ('YBC_NEW_REGISTER', '{}', 'NEW', 0),
                        ('YBC_NEW_REGISTER', '{}', 'NEW', 0);
                    '''
                )
                print("insert rows: ", i_rows)
            time.sleep(0.5)
finally:
    connection.close()
